import LoadingController from "../loading/LoadingController";
import ModuleManager, { ModuleID } from "../../cfw/module/ModuleManager";
import BaseController from "../../cfw/mvc/BaseController";
import GiftMgr from "./model/GiftMgr";
import GiftItemModel from "./model/GiftItemModel";
import { ItemState, UIIndex } from "../../cfw/tools/Define";
import SDKManager from "../../sdk/SDKManager";
import { ResultState } from "../../sdk/SDKConfig";
import LobbyController from "../lobby/LobbyController";

export default class GiftC extends BaseController {

    private static ins: GiftC;

    static instance() {
        if (!this.ins) {
            this.ins = new GiftC()
        }
        return this.ins;
    }

    intoLayer() {
        this.pushView('prefabs/gift/GiftView', 'GiftView', GiftMgr.instance(), ModuleManager.getLoader(ModuleID.GIFT), UIIndex.ROOT)
    }

    intoShowView(m: GiftItemModel) {
        this.pushView('prefabs/gift/GiftShowView', 'GiftShowView', m, ModuleManager.getLoader(ModuleID.GIFT))
    }

    clickItem(m: GiftItemModel) {
        switch (m.getState()) {
            case ItemState.NOT_GET:
                SDKManager.getChannel().showRewardAd(0, (state: ResultState) => {
                    if (state) {
                        this.intoShowView(m)
                    }
                })
                break;
            case ItemState.CAN_GET:
                this.intoShowView(m)
                break;
            case ItemState.GOT:
                if (m.getAudio() != '') {
                    ModuleManager.getAudio(ModuleID.GIFT).playSound(m.getAudio())
                }
                break;
        }
    }

    intoLobby() {
        LobbyController.instance().intoLayer()
    }
}
